package com.schoolET.controller.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.schoolET.entity.GoodsType;
import com.schoolET.entity.dto.GoodsTypeInfoDTO;
import com.schoolET.error.costom.NoGoodsTypeException;
import com.schoolET.result.MyPage;
import com.schoolET.result.Result;
import com.schoolET.service.admin.IGoodsTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author: lin
 * @className: GoodsTypeManageController
 * @createTime: 2024-06-04  17:02
 * @description: 商品类型管理接口
 */
@Api(tags = "商品类型管理")
@RestController
@RequestMapping("/schoolET")
public class GoodsTypeManageController {

    @Resource
    private IGoodsTypeService goodsTypeService;

    @ApiOperationSupport(author = "lin")
    @ApiOperation(value = "查看商品类型")
    @SaCheckLogin
    @SaCheckRole("admin")
    @PostMapping("/typeList")
    public Result typeList(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize) {
        // 检查pageNum是否为负数，如果是，则设为默认值1
        pageNum = pageNum < 0 ? 1 : pageNum;
        // 检查pageSize是否为负数，如果是，则设为默认值5
        pageSize = pageSize < 0 ? 5 : pageSize;
        Page<GoodsTypeInfoDTO> goodsTypePage = goodsTypeService.inquireGoodsType(pageNum, pageSize);
        MyPage myPage = MyPage.page(goodsTypePage);
        return Result.success(myPage).msg("查询成功");
    }

    @ApiOperationSupport(author = "lin")
    @ApiOperation(value = "添加商品类型")
    @SaCheckLogin
    @SaCheckRole("admin")
    @PostMapping("/typeIns")
    public Result typeIns(String type) {
        goodsTypeService.addGoodsType(type);
        return Result.success().msg("添加成功");
    }

    @ApiOperationSupport(author = "lin")
    @ApiOperation(value = "删除商品类型")
    @SaCheckLogin
    @SaCheckRole("admin")
    @PostMapping("/typeDel/{id}")
    public Result typeDel(@PathVariable Integer id) {
        GoodsType byId = goodsTypeService.getById(id);
        if (byId == null) {
            throw new NoGoodsTypeException();
        }
        goodsTypeService.removeById(id);
        return Result.success().msg("删除成功");
    }

}
